/**
 * 
 */
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.HOC_KY_KHHTM;
import net.sf.json.JSONObject;
import dao.HOC_KY_KHHTD;
@WebServlet("/HOC_KY_KHHTServlet")
public class HOC_KY_KHHTServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HOC_KY_KHHTD dao;
       
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public HOC_KY_KHHTServlet() throws Exception {
        super();
        this.dao = new HOC_KY_KHHTD();
    }

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		String flag = request.getParameter("flag");
		try {
			if (flag == null || "".equals(flag.trim())) {
				showHOC_KY_KHHT(request, response);
			}else if ("add".equals(flag.trim())) {
				String stt= request.getParameter("stt");
				String ten= request.getParameter("ten");
				String id_khht_mau = request.getParameter("id_khht_mau");
				String so_tc= request.getParameter("so_tc");
				String so_tc_bb= request.getParameter("so_tc_bb");
				String so_tc_tc= request.getParameter("so_tc_tc");
				String so_tc_dk= request.getParameter("so_tc_dk");
				addHOC_KY_KHHT(stt,ten,Integer.parseInt(id_khht_mau),so_tc,so_tc_bb,so_tc_tc,so_tc_dk,request,response);
			}else if ("edit".equals(flag.trim())) {
				String id_hoc_ky_khht = request.getParameter("id_hoc_ky_khht");
				String stt= request.getParameter("stt");
				String ten= request.getParameter("ten");
				String id_khht_mau = request.getParameter("id_khht_mau");
				String so_tc= request.getParameter("so_tc");
				String so_tc_bb= request.getParameter("so_tc_bb");
				String so_tc_tc= request.getParameter("so_tc_tc");
				String so_tc_dk= request.getParameter("so_tc_dk");
				editHOC_KY_KHHT(Integer.parseInt(id_hoc_ky_khht),stt,ten,Integer.parseInt(id_khht_mau),so_tc,so_tc_bb,so_tc_tc,so_tc_dk, request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id = request.getParameter("id").split(",");
				for(int i = 0; i<id.length; i++){
					deleteHOC_KY_KHHT(Integer.parseInt(id[i]), request, response);
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function show CTDT
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showHOC_KY_KHHT(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		String id=request.getParameter("id")==null?"0":request.getParameter("id");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.searchHK_KHHT(search).size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls(Integer.parseInt(id), offSet, rows));
		if(search!=""){
			returnMap.put("rows", dao.getAlls(search, offSet, rows));
		}
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add CTDT
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addHOC_KY_KHHT(String stt, String ten, int id_khht_mau, String so_tc, String so_tc_bb, String so_tc_tc, String so_tc_dk, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		HOC_KY_KHHTM c = new HOC_KY_KHHTM();
		c.setStt(stt);
		c.setTen(ten);
		c.setId_khht_mau(id_khht_mau);
		c.setSo_tc(so_tc);
		c.setSo_tc_bb(so_tc_bb);	
		c.setSo_tc_tc(so_tc_tc);
		c.setSo_tc_dk(so_tc_dk);
		if(dao.Insert(c)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function edit CTDT
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void editHOC_KY_KHHT(int id_hoc_ky_khht, String stt, String ten, int id_khht_mau, String so_tc, String so_tc_bb, String so_tc_tc, String so_tc_dk, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		HOC_KY_KHHTM c = new HOC_KY_KHHTM();
		c.setId_hoc_ky_khht(id_hoc_ky_khht);
		c.setStt(stt);
		c.setTen(ten);
		c.setId_khht_mau(id_khht_mau);
		c.setSo_tc(so_tc);
		c.setSo_tc_bb(so_tc_bb);	
		c.setSo_tc_tc(so_tc_tc);
		c.setSo_tc_dk(so_tc_dk);
		if(dao.Update(c)==true){
			map.put("success", "true");
			map.put("msg", "Sửa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi sửa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete CTDT
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteHOC_KY_KHHT(int id, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
}